package expertise.titan.dao.vertex.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.thinkaurelius.titan.core.TitanTransaction;
import com.thinkaurelius.titan.diskstorage.StorageException;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;

import expertise.titan.dao.exception.DaoConfigurationException;
import expertise.titan.dao.vertex.AbstractVertexDaoImpl;
import expertise.titan.dao.vertex.EmployeeDao;
import expertise.titan.dao.vertex.SkillDao;
import expertise.titan.schema.edge.HasExperienceWith;
import expertise.titan.schema.vertex.Employee;
import expertise.titan.schema.vertex.Skill;

@Component
public class EmployeeDaoImpl extends AbstractVertexDaoImpl<Employee> implements EmployeeDao {

    @Resource
    private SkillDao skillDao;

    public EmployeeDaoImpl() throws DaoConfigurationException {
        super("employee", Employee.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void lazyLoadEmployeeSkills(final Employee employee, final TitanTransaction transaction) throws StorageException {
        final Vertex v = transaction.getVertex(titanEnvironment.vertexNameKey(), employee.getName());
        for (final Edge e : v.getEdges(Direction.OUT, HasExperienceWith.TYPE)) {

            Vertex vSkill = e.getVertex(Direction.IN);
            final Skill s = skillDao.createSchemaFromVertex(vSkill);
            // TODO add years of experience from the edge
            employee.getSkills().add(s);
        }
    }

}
